package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.enums.EsAliasEnum;
import com.xbongbong.paas.enums.SubEsAliasEnum;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.service.FieldIndexService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.form.pojo.FieldEsIndexPoJo;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/04/22 16:07
 */
@Service("fieldIndexService")
public class FieldIndexServiceImpl implements FieldIndexService {

    @Resource
    private CompanyConfigModel companyConfigModel;


    @Override
    public Map<String, Map<String, FieldEsIndexPoJo>>  getFieldIndexNum(String corpid, Integer saasMark, Integer businessType, Integer distributorMark, Long formId) throws XbbException {
        try {
            List<EsAliasEnum> list = EsAliasEnum.getEsAliasList();
            Map<String, FieldEsIndexPoJo> fieldEsIndexPoJoMap = new HashMap<>(list.size());
            String value = companyConfigModel.getByConfigAlias(CompanyConfigEnum.ELASTICSEARCH_INDEX.getAlias(), CompanyConfigEnum.ELASTICSEARCH_INDEX.getAlias(), "0", "0", RedisTimeConstant.TEN_MINUTES);
            Set<String> corpidSet = new HashSet<>();
            if (StringUtil.isNotEmpty(value)) {
                corpidSet = new HashSet<>(JSON.parseArray(value, String.class));
            }
            for(EsAliasEnum es: list) {
                Integer total = es.getTotal();
                if (corpidSet.contains(corpid) && Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)
                        && Objects.equals(DistributorMarkEnum.OTHER.getCode(), distributorMark)) {
                    total = es.getSeparateIndexMax();
                }
                Integer customize = es.getSaasMax();
                if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)) {
                    customize = es.getMax();
                }
                FieldEsIndexPoJo fieldEsIndexPoJo = new FieldEsIndexPoJo(es.getEsAlias(), total, customize, es.getTotal()-customize);
                fieldEsIndexPoJoMap.put(es.getEsAlias(), fieldEsIndexPoJo);
            }
            Map<String, Map<String, FieldEsIndexPoJo>> fieldMap = new HashMap<>(2);
            fieldMap.put("field", fieldEsIndexPoJoMap);

            List<SubEsAliasEnum> subList = SubEsAliasEnum.getEsSubAliasList();
            Map<String, FieldEsIndexPoJo> subFieldEsIndexPoJoMap = new HashMap<>(subList.size());
            for(SubEsAliasEnum es: subList) {
                FieldEsIndexPoJo fieldEsIndexPoJo = new FieldEsIndexPoJo(es.getEsAlias(), es.getMax(), es.getMax(), 0);
                subFieldEsIndexPoJoMap.put(es.getEsAlias(), fieldEsIndexPoJo);
            }
            fieldMap.put("subField", subFieldEsIndexPoJoMap);
            return fieldMap;
        } catch (Exception e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
